#include "commu.h"
#include <stdio.h>
#include <string.h>
#include "controller.h"
struct COMM comm;

uint8_t dmaCh2Buf[COMM_BUFFSIZE];
uint8_t uploadEn = 1;
uint8_t preTempSmp;
uint8_t preTempSet;
uint8_t prePowerAC;
uint8_t preModeAC;
uint8_t preSpeedSet;
extern uint8_t local_mac[6];
extern uint8_t gate_mac[6];
extern void CmdTest_MSE_GET_MAC(void);
extern void CmdTest_MSE_GET_DID(void);
extern void CmdTest_MSE_GET_CC0MAC(void);
extern void params_upload(void);

void comm_uart_init(void)
{
	/* 开启时钟 */
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_GPIOB);
	rcu_periph_clock_enable(RCU_AF);
	gpio_pin_remap_config(GPIO_USART0_REMAP,ENABLE);
	rcu_periph_clock_enable(RCU_USART0);
	rcu_periph_clock_enable(RCU_DMA0);
	/* USART_GPIO配置 */
	gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
	gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
	/* USART配置 */
	usart_baudrate_set(USART0, 115200);
	usart_parity_config(USART0, USART_PM_NONE);
	usart_word_length_set(USART0, USART_WL_8BIT);
	usart_stop_bit_set(USART0, USART_STB_1BIT);
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
	usart_receive_config(USART0, USART_RECEIVE_ENABLE);
	nvic_irq_enable(USART0_IRQn, 2, 0);
	usart_interrupt_enable(USART0, USART_INT_IDLE);
	usart_dma_receive_config(USART0, USART_RECEIVE_DMA_ENABLE);
	usart_enable(USART0);
	/* DMA配置 */
	dma_parameter_struct dmaInitStruct;
	dmaInitStruct.periph_addr = (uint32_t)(&USART_DATA(USART0));
	dmaInitStruct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
	dmaInitStruct.memory_addr = (uint32_t)dmaCh2Buf;
	dmaInitStruct.memory_width = DMA_PERIPHERAL_WIDTH_8BIT;
	dmaInitStruct.number = sizeof(dmaCh2Buf);
	dmaInitStruct.priority = DMA_PRIORITY_HIGH;
	dmaInitStruct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dmaInitStruct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dmaInitStruct.direction = DMA_PERIPHERAL_TO_MEMORY;
	dma_init(DMA0, DMA_CH4, &dmaInitStruct);
	dma_circulation_disable(DMA0, DMA_CH4);
	dma_memory_to_memory_disable(DMA0, DMA_CH4);
	dma_channel_enable(DMA0, DMA_CH4);
}

void comm_send_bytes(uint8_t *buf, uint16_t len)
{
	while(len--)
	{
		usart_data_transmit(USART0, *buf++);
		/* 等待发送完毕 */
		while(usart_flag_get(USART0, USART_FLAG_TC) == RESET);
	}
}

__attribute__((weak)) void comm_receiver_callback(uint8_t *buf, uint16_t len)
{
	printf("%s\r\n", buf);
}

void comm_receiver_process(void)
{
	if(comm.len != 0)
	{
		comm_receiver_callback(comm.buf, comm.len);
		
		comm.len = 0;
	}
}

void comm_sender_process(void)
{
	static uint8_t uploadFlag;
	static uint8_t uploadCnt;
	uint8_t mac_init[6] = {0};
	uint8_t tempSmp = get_tempSmp();		/* 室温 */
	uint8_t tempSet = get_tempSet_ac();		/* 设定温度 */
	uint8_t powerAC = get_power_ac();		/* 空调电源开关状态 */
	uint8_t modeAC = get_ac_mode();			/* 空调模式 */
	uint8_t speedSet = get_speedSet_ac();	/* 空调风机设定风速 */

	if(strncmp((char *)mac_init, (char *)local_mac, 6) == 0)
	{
		CmdTest_MSE_GET_MAC();
	}
	else if(strncmp((char *)mac_init, (char *)gate_mac, 6) == 0)
	{
		CmdTest_MSE_GET_CC0MAC();
	}
	else if(get_net_state() == 0)
	{
		CmdTest_MSE_GET_DID();
	}
	else
	{
		if(uploadEn)
		{
			if(++uploadCnt == 3)
			{
				uploadCnt = 0;
				if(preTempSet != tempSet)
				{
					preTempSet = tempSet;
					uploadFlag = 1;
				}
				if(preTempSmp != tempSmp)
				{
					preTempSmp = tempSmp;
					uploadFlag = 1;
				}
				if(prePowerAC != powerAC)
				{
					prePowerAC = powerAC;
					uploadFlag = 1;
				}
				if(preModeAC != modeAC)
				{
					preModeAC = modeAC;
					uploadFlag = 1;
				}
				if(preSpeedSet != speedSet)
				{
					preSpeedSet = speedSet;
					uploadFlag = 1;
				}
			}

			if(uploadFlag)
			{
				uploadFlag = 0;
				params_upload();
			}
		}
	}
}

void USART0_IRQHandler(void)	
{
	//空闲中断
    if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) 
	{
		/* 清除空闲中断标志位 */
		usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);	
		/* 清除接收完成标志位 */
		usart_data_receive(USART0);
		/* 关闭DMA传输 */
		dma_channel_disable(DMA0, DMA_CH4);
		
		comm.len = sizeof(dmaCh2Buf) - dma_transfer_number_get(DMA0, DMA_CH4);
		/* 转存数据到待处理数据缓冲区 */
		memcpy(comm.buf, dmaCh2Buf, comm.len);
		
		/* 重新设置DMA传输 */
		dma_memory_address_config(DMA0, DMA_CH4, (uint32_t)dmaCh2Buf);
		dma_transfer_number_config(DMA0, DMA_CH4, sizeof(dmaCh2Buf));
		/* 开启DMA传输 */
		dma_channel_enable(DMA0, DMA_CH4);
    }
}

